1. Field of the Invention
The invention relates to computing systems and, more particularly, to detecting and fixing prematurely freed objects.
2. Description of the Related Art
Computer programming languages such a C and C++ allow programmers to manage memory by enabling them to explicitly allocate and free portions of memory during execution of a program. While this ability to explicitly allocate and free memory may simplify many programming tasks, it does present certain challenges as well. For example, once memory has been allocated, the programmer must ensure that it is not freed again until the program is done using it. Freeing a portion of memory before the program is done using it is sometime referred to as a “premature free”. In addition, once the program is finished using allocated memory, it should be freed and not retained in an allocated state. Otherwise, the memory which was allocated will remain unavailable for reuse by that or other programs.
As those skilled in the art will appreciate, a variety of factors may increase the difficulty of avoiding errors in the management of memory. For example, the difficult in properly managing memory may increase as the size of a program grows. In addition, memory may be allocated in one module of a program, used in other modules of the program, and freed in still other modules. The fact that these modules may be written by different programmers can further complicate matters. Further, the difficulty in managing memory may also increase as a program ages. Most programs undergo revision in order to fix bugs or add new features. Given that the people who do the revisions may be different from those who wrote the original program, the manner in which the revised program manages memory may be different from that contemplated by the original authors of the program.
One way of solving some of the problems posed by programs which manage their own memory is to reserve all freeing of memory to a special program called a garbage collector which frees blocks of memory that are no longer being used by a program. Generally speaking, garbage collectors work by detecting portions of memory that no longer have pointers pointing to them (i.e., are “unreachable”). In this manner, the garbage collector may identify and help resolve memory leaks. However, while the use of automated garbage collectors may ease some of the problems of managing memory, garbage collection mechanisms—when they are available—may involve a significant amount of overhead. Consequently, it may not be desirable to use automated garbage collection for a particular application.
In addition to the above, garbage collectors generally do not address the problem of prematurely freed memory. As noted above, a premature free results when a portion of memory is freed while the portion of memory is still in use by the program. Once memory is free, it generally becomes part of the system heap and is made available for reallocation. A subsequent attempted access to memory that has already been freed may access data in an unrelated part of the program. Consequently, premature frees may result in corrupted data. In addition, locating the root cause of such data corruption can be very difficult to debug.
Accordingly, an effective method and mechanism for managing premature frees is desired.